package services;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Vector;

import model.*;

import java.util.ArrayList;

import javax.swing.table.DefaultTableModel;

import controllers.GenericConector;


public class ClientesServices {
	private List<Clientes> lista;
	private GenericConector gc;
	private ResultSet rs;
	
	public List<Clientes> GetClientesAll() throws SQLException{
		gc = new GenericConector();
		rs = gc.getResultSet("SELECT * FROM Clientes");
		
		lista = new ArrayList<Clientes>();
		
		while(rs.next()){
			lista.add(new Clientes(rs.getInt(""),rs.getString("") , rs.getDate("")));
		}
		
		return lista;
	}
	
	public void SaveCliente(Clientes cli) throws SQLException
	{
		gc = new GenericConector();
	//guarda el nombre. id=autonumerico. 
		gc.ejecutar("insert into Grupo_1_DB.clientes (usuario) values ('"+cli.getNombre()+"')");
		
	}
	
	public List<Clientes> GetClientesArchivo(){
		ObjectInputStream dos = null;
		try {
			dos = new ObjectInputStream(new BufferedInputStream(
					new FileInputStream("clientes.txt")));
			while (true) {
				try {
					lista = (List<Clientes>) dos.readObject();
					
				} catch (ClassNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		catch (Exception e) {}
		
		finally {			
			if (dos != null) {
				try {
					dos.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			else{
				return lista;
			}
		}
		return lista;
	}

}
